package com.briup.homework.chap12;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test03_Client {
    public static void main(String[] args) throws Exception {
        //1.定义Scanner类和要录入的字符串
        Scanner sc = new Scanner(System.in);
        //2.定义集合存放老师信息
        List<Teacher> teachers = new ArrayList<>();
        //3.逐行录入老师信息，直到遇到quit结束录入
        while(true){
            String s = sc.nextLine();
            if("quit".equals(s)){
                break;
            }
            //3.1 拆分录入的字符串
            String[] split = s.split("-");
            //3.2 创建Teacher对象，并为对应属性赋值
            Teacher teacher = new Teacher(split[0],Integer.parseInt(split[1]),Double.parseDouble(split[2]));
            //3.3 将老师对象添加到集合中
            teachers.add(teacher);
        }
        //4.搭建TCP客户端
        Socket socket = new Socket("127.0.0.1",8002);
        //5.发送集合对象
        // 5.1 获取输出流，并包装成对象流
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        // 5.2 发送集合对象
        oos.writeObject(teachers);
        //6.从服务器接收数据
        // 6.1 获取输入流并包装成缓冲流
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        socket.getInputStream()));
        // 6.2 读取信息并在控制台答应
        System.out.println(br.readLine());
        //7.关闭资源
        br.close();
        oos.close();
        socket.close();
    }
}
